<!DOCTYPE html>
<html>
  <head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta content="yes" name="apple-mobile-web-app-capable" />
  <meta content="black" name="apple-mobile-web-app-status-bar-style" />
  <meta name="referrer" content="never">
  <meta name="keywords" content="">
  <meta name="description" content="">
  <meta name="author" content="kveln">
  <title>Jenkines Pipline CI/CD 发布至k8s  | John Wong&#39;s Blog</title>
  <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
  <!-- <link href="https://blog.jwangkun.com/media/css/bootstrap.min.css" rel="stylesheet"> -->
  <!--  <link href="https://blog.jwangkun.com/media/css/all.min.css" rel="stylesheet" type="text/css"> -->
  <link href="https://cdn.bootcss.com/font-awesome/5.11.2/css/all.min.css" rel="stylesheet">
  <link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
  <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
  <link rel="alternate" type="application/rss+xml" title="Jenkines Pipline CI/CD 发布至k8s  | John Wong&#39;s Blog » Feed" href="https://blog.jwangkun.com/atom.xml">
  <link rel="stylesheet"href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.15.10/build/styles/androidstudio.min.css">
  <link href="https://blog.jwangkun.com/styles/main.css" rel="stylesheet">
  <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.15.10/build/highlight.min.js"></script>
  <!-- <script src="https://blog.jwangkun.com/media/scripts/jquery.min.js"></script> -->
  <script>hljs.initHighlightingOnLoad();</script>
  

    <meta property="og:description" content="Jenkines Pipline CI/CD 发布至k8s "/>
    <meta property="og:url" content="https://blog.jwangkun.com/post/WhrC1yTsk/"/>
    <meta property="og:locale" content="zh-CN"/>
    <meta property="og:type" content="website"/>
    <meta property="og:site_name" content="John Wong&#39;s Blog"/>
  </head>
  <body>
  	<!-- Navigation -->
  <nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
    <div class="container">
      <a class="navbar-brand" href="https://blog.jwangkun.com">John Wong&#39;s Blog</a>
      <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
        Menu
        <i class="fas fa-bars"></i>
      </button>
      <div class="collapse navbar-collapse" id="navbarResponsive">
        <ul class="navbar-nav ml-auto">
          
          <li class="nav-item">
              
              <a class="nav-link" href="/">首页</a>
              
          </li>
          
          <li class="nav-item">
              
              <a class="nav-link" href="/archives">归档</a>
              
          </li>
          
          <li class="nav-item">
              
              <a class="nav-link" href="/tags">标签</a>
              
          </li>
          
          <li class="nav-item">
              
              <a class="nav-link" href="/post/about">关于</a>
              
          </li>
          
        </ul>
      </div>
    </div>
  </nav>
  <!-- Page Header -->
  <header class="masthead" style="background-image: url('https://blog.jwangkun.com/media/images/home-bg.jpg')">
    <div class="overlay"></div>
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <div class="post-heading">
          	<span class="tags">
          	 
        </span>
            <h1>Jenkines Pipline CI/CD 发布至k8s </h1>
            <span class="meta">
            	Posted on
              2020-10-15，16 min read
            </span>
          </div>
        </div>
      </div>
    </div>
  </header>

  <!-- Post Content -->
  <article>
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <p>pipeline是一套jenkins官方提供的插件，它可以用来在jenkins中实现和集成连续交付 <strong>用户可以利用Pipeline的许多功能：</strong></p>
<p>代码：pipeline在代码中实现，通常检查到源代码控制，使团队能够编辑，审查和迭代其交付管道。 持久：pipeline可以在Jenkins master的计划内和计划外重启中存活。 Pausable：在继续pipeline运行之前，pipeline可以选择停止并等待人工输入或批准。 多功能：pipeline支持复杂的实际CD要求，包括并行分叉/连接，循环和执行工作的能力。 可扩展：Pipeline插件支持其DSL的自定义扩展 和多个与其他插件集成的选项</p>
<h2 id="二-k8s实现集成部署交付">二、k8s实现集成/部署/交付</h2>
<figure data-type="image" tabindex="1"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c201e1b59?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<h2 id="三-创建一个gitlab的测试项目网上找的简单代码测试">三、创建一个gitlab的测试项目（网上找的简单代码测试）</h2>
<h3 id="1-测试代码">1、测试代码</h3>
<p>代码结构</p>
<pre><code>[root@node2 test1]# tree 
.
├── pom.xml
└── src
    └── main
        └── java
            └── hello
                ├── Greeter.java
                └── HelloWorld.java
复制代码
</code></pre>
<p><strong>HelloWorld.java</strong></p>
<pre><code>package hello;

public class HelloWorld {
    public static void main(String[] args) {
        Greeter greeter = new Greeter();
        System.out.println(greeter.sayHello());
    }
}
复制代码
</code></pre>
<p><strong>Greeter.java</strong></p>
<pre><code>package hello;

public class Greeter {
    public String sayHello() {
        return &quot;Hello world!&quot;;
    }
}
复制代码
</code></pre>
<p><strong>pom.xml</strong></p>
<pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;gs-maven&lt;/artifactId&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
    &lt;version&gt;0.1.0&lt;/version&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
                &lt;version&gt;2.1&lt;/version&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;shade&lt;/goal&gt;
                        &lt;/goals&gt;
                        &lt;configuration&gt;
                            &lt;transformers&gt;
                                &lt;transformer
                                    implementation=&quot;org.apache.maven.plugins.shade.resource.ManifestResourceTransformer&quot;&gt;
                                    &lt;mainClass&gt;hello.HelloWorld&lt;/mainClass&gt;
                                &lt;/transformer&gt;
                            &lt;/transformers&gt;
                        &lt;/configuration&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;

复制代码
</code></pre>
<h3 id="2-git的测试项目">2、git的测试项目</h3>
<figure data-type="image" tabindex="2"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c21c88495?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<h2 id="四-jenkins配置">四、jenkins配置</h2>
<h3 id="1-插件安装">1、插件安装</h3>
<p>安装好git、GitLab Plugin、Gitlab Hook Plugin、Gitlab API Plugin、Pipeline、Kubernetes plugin插件。</p>
<h3 id="2-在jenkins中配置kubernetes">2、在jenkins中配置kubernetes</h3>
<figure data-type="image" tabindex="3"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c25a33197?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<h3 id="3-简单测试创建一个pipline项目">3、简单测试（创建一个Pipline项目）</h3>
<p>测试的Pipline脚本</p>
<pre><code>def label = &quot;mypod-${UUID.randomUUID().toString()}&quot;
podTemplate(label: 'label', cloud: 'kubernetes') {
    node('label') {
        stage('Run shell') {
            sh 'echo hello world'
        }
    }
}
复制代码
</code></pre>
<figure data-type="image" tabindex="4"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c264f2fe9?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<figure data-type="image" tabindex="5"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0e46b252d026?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<figure data-type="image" tabindex="6"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0e46b252d026?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<figure data-type="image" tabindex="7"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c27fbfdd2?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<h3 id="4-pipline中配置gi凭证">4、pipline中配置gi凭证</h3>
<p><strong>创建凭证，username是gitlab的账号，password是gitlab账号对应的密码。</strong></p>
<figure data-type="image" tabindex="8"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c428f0129?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<h3 id="5-pipline中视图git凭证">5、Pipline中视图git凭证</h3>
<figure data-type="image" tabindex="9"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c579a52d7?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<figure data-type="image" tabindex="10"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c584a1987?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<h2 id=""></h2>
<h3 id="6-重新构建jnlp-slave镜像添加maven">6、重新构建jnlp-slave镜像（添加maven）</h3>
<pre><code>mkdir myjenkins
cd myjenkins/
wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
tar xf apache-maven-3.5.4-bin.tar.gz

[root@node2 myjenkins]# cat Dockerfile 
FROM jenkins/jnlp-slave:3.27-1
LABEL maintainer=&quot;maven@qq.com&quot;
ENV MAVEN_HOME /usr/local/maven
ENV PATH=$PATH:$MAVEN_HOME/bin

COPY apache-maven-3.5.4  /usr/local/maven
[root@node2 myjenkins]# docker build  -t myjnlp-slave:1.0 ./
复制代码
</code></pre>
<h3 id="7-配置好镜像仓库我使用的aliyun">7、配置好镜像仓库（我使用的aliyun）</h3>
<figure data-type="image" tabindex="11"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c5d5b9aae?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<h3 id="8-把dokcer组的gid改成10000容器中jenkins账号的ugid">8、把dokcer组的gid改成10000（容器中jenkins账号的ugid）</h3>
<pre><code>groupmod  -g10000  docker
chown root.docker /var/run/docker.sock
复制代码
</code></pre>
<h3 id="9-配置gitlab和jenkins的交互我使用的webhook方式并设置提交tag就触发构建">9、配置gitlab和jenkins的交互（我使用的webhook方式，并设置提交tag就触发构建）</h3>
<h3 id="10-重新更改pipline脚本使执行jenkins构建后能直接创建镜像并提交到镜像仓库">10、重新更改Pipline脚本，使执行jenkins构建后能直接创建镜像并提交到镜像仓库</h3>
<p><strong>Pipline脚本</strong></p>
<pre><code>def label = &quot;mypod-${UUID.randomUUID().toString()}&quot;
def registry = &quot;registry-vpc.cn-hongkong.aliyuncs.com&quot;
def app_name = &quot;javatest&quot;
def namespace = &quot;aliyun-zhang&quot;
def username = &quot;xxxxxxxxxxxxxxxxxx&quot;
def regpass = &quot;xxxxxxxxxxxxxxxxxxx&quot;


podTemplate(label: 'label', cloud: 'kubernetes', containers: [
    containerTemplate(
        name: 'jnlp', 
        image: 'myjnlp-slave:1.0'
    ),
],
  volumes: [
    hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
    hostPathVolume(mountPath: '/usr/bin/docker', hostPath: '/usr/bin/docker')
],)
{
    node('label') {
        stage('Task') {
            stage('拉取代码') {
                git credentialsId: 'gitlab-auth', url: 'http://mygitlab-gitlab-ce.default.svc.cluster.local/root/test1.git'
                def mytag = sh returnStdout: true, script: 'git describe --always --tag'
                sh &quot;git checkout -b $mytag&quot;
            }
            stage('编译打包') {
                sh &quot;mvn clean package -Dmaven.test.skip=true&quot;
            }
            
            stage('构建上传镜像') {
                def mytag = sh returnStdout: true, script: 'git describe --always --tag'
                def image_name = &quot;${app_name}:${mytag}&quot;.minus(&quot;\n&quot;)
                sh label: '', script: '''              echo \'
                    FROM tomcat:latest 
                    ADD pom.xml /usr/local/tomcat/webapps/ 
                    \'  Dockerfile
                ''' 
                    
                sh &quot;&quot;&quot;
                    docker build  -t &quot;${registry}/${namespace}/${image_name}&quot; ./
                    docker login -u ${username} -p \&quot;${regpass}\&quot; ${registry}
                    docker push ${registry}/${namespace}/${image_name}
                    
                &quot;&quot;&quot;
                
            }            
        }
    }
}
复制代码
</code></pre>
<h3 id="11-jenkins构建测试与查看">11、jenkins构建测试与查看</h3>
<figure data-type="image" tabindex="12"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c58fcb584?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<figure data-type="image" tabindex="13"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c6fb0cc5a?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<h2 id="五-自动部署到k8s集群">五、自动部署到k8s集群</h2>
<h3 id="1-jenkins下载kubernetes-continuous">1、jenkins下载kubernetes continuous</h3>
<p>deploy插件（官方文档：<a href="https://plugins.jenkins.io/kubernetes-cd%EF%BC%89">plugins.jenkins.io/kubernetes-…</a></p>
<figure data-type="image" tabindex="14"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c89243367?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<h3 id="2-配置kubeconfig凭证">2、配置kubeconfig凭证</h3>
<p>添加 kubeconfig 文件，我选择的是直接粘贴文件内容（master端的.kube/config文件）</p>
<figure data-type="image" tabindex="15"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c8a36a07c?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<h3 id="3-记录凭证id值">3、记录凭证ID值</h3>
<figure data-type="image" tabindex="16"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c8a88ec9c?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<h3 id="4-创建一个deployyaml文件并把文件上传到项目的git中">4、创建一个deploy.yaml文件，并把文件上传到项目的git中</h3>
<pre><code>[root@k8s-m ~]# cat java-deploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mydeploy
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      test_node: k8s-node
  template:
    metadata:
      labels:
        test_node: k8s-node
    spec:
      containers:
      - name: myjava-server
        image: tomcat:latest
        ports:
        - name: http
          containerPort: 8080
复制代码
</code></pre>
<figure data-type="image" tabindex="17"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c8efcc776?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<h3 id="5-更改pipline脚本内容">5、更改Pipline脚本内容</h3>
<pre><code>def label = &quot;mypod-${UUID.randomUUID().toString()}&quot;
def registry = &quot;registry-vpc.cn-hongkong.aliyuncs.com&quot;
def app_name = &quot;javatest&quot;
def namespace = &quot;aliyun-zhang&quot;
def username = &quot;xxxxxxxxxxxx&quot;
def regpass = &quot;xxxxxxxxx&quot;
def k8s_auth = &quot;5ce0993e-e2e9-4126-a910-2acd0a77fefb&quot;


podTemplate(label: 'label', cloud: 'kubernetes', containers: [
    containerTemplate(
        name: 'jnlp', 
        image: 'myjnlp-slave:1.0'
    ),
],
  volumes: [
    hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
    hostPathVolume(mountPath: '/usr/bin/docker', hostPath: '/usr/bin/docker')
],)
{
    node('label') {
        stage('Task') {
            stage('拉取代码') {
                git credentialsId: 'gitlab-auth', url: 'http://mygitlab-gitlab-ce.default.svc.cluster.local/root/test1.git'
                def mytag = sh returnStdout: true, script: 'git describe --always --tag'
                sh &quot;git checkout -b $mytag&quot;
                echo &quot;mytag $mytag ${mytag} ----&quot;
            }
            stage('编译打包') {
                sh &quot;mvn clean package -Dmaven.test.skip=true&quot;
            }
            
            stage('构建上传镜像') {
                def mytag = sh returnStdout: true, script: 'git describe --always --tag'
                def image_name = &quot;${app_name}:${mytag}&quot;.minus(&quot;\n&quot;)
                
                echo &quot;image_name $image_name&quot;
                sh label: '', script: '''              echo \'
                    FROM tomcat:latest 
                    ADD target/*.jar /usr/local/tomcat/webapps/ 
                    \'  Dockerfile
                ''' 
                    
                sh &quot;&quot;&quot;
                    docker build  -t &quot;${registry}/${namespace}/${image_name}&quot; ./
                    docker login -u ${username} -p \&quot;${regpass}\&quot; ${registry}
                    docker push ${registry}/${namespace}/${image_name}
                    
                &quot;&quot;&quot;
            }
             stage('部署到K8S'){
                def mytag = sh returnStdout: true, script: 'git describe --always --tag'
                def image_name = &quot;${app_name}:${mytag}&quot;.minus(&quot;\n&quot;)
                sh &quot;&quot;&quot;
                sed -i 's#tomcat:latest#${registry}/${namespace}/${image_name}#' java-deploy.yaml
                &quot;&quot;&quot;
                kubernetesDeploy configs: 'java-deploy.yaml', kubeconfigId: &quot;${k8s_auth}&quot;
      }
        }
    }
}
复制代码
</code></pre>
<h3 id="6-构建测试">6、构建测试</h3>
<h3 id="我配置了jenkins和gitlab的webhook配置提交tag后会自动构建到部署到k8s集群">我配置了jenkins和gitlab的webhook配置,提交tag后会自动构建到部署到k8s集群</h3>
<p>测试</p>
<pre><code>[root@node2 test1]# echo 123 &gt;&gt;README.md 
[root@node2 test1]# git commit -a -m '123'
[master bda4c6f] 123
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@node2 test1]# git tag  10.0 
[root@node2 test1]# git push origin master 10.0
Username for 'http://10.101.58.237': root
Password for 'http://root@10.101.58.237': 
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 347 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://10.101.58.237/root/test1.git
   da0fbc3..bda4c6f  master - master
 * [new tag]         10.0 - 10.0
复制代码
</code></pre>
<p>jenkins构建过程</p>
<figure data-type="image" tabindex="18"><img src="https://user-gold-cdn.xitu.io/2019/8/27/16cd0b4c9a56f59e?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="img" loading="lazy"></figure>
<p><strong>k8s集群查看</strong></p>
<pre><code>[root@k8s-m ~]# kubectl get deploy  mydeploy
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
mydeploy   2/2     2            2           64s
[root@k8s-m ~]# kubectl get pod  
NAME                                   READY   STATUS    RESTARTS   AGE
mydeploy-746bb8db64-4fpvz              1/1     Running   0          69s
mydeploy-746bb8db64-pp7tw              1/1     Running   0          69s

[root@k8s-m ~]# kubectl get pod mydeploy-746bb8db64-4fpvz  -o yaml|grep image: 
  - image: registry-vpc.cn-hongkong.aliyuncs.com/aliyun-zhang/javatest:10.0
    image: registry-vpc.cn-hongkong.aliyuncs.com/aliyun-zhang/javatest:10.0
复制代码
</code></pre>
<h2 id="六-版本回滚">六、版本回滚</h2>
<p>基于k8s镜像的回滚还是比较简单的 <strong>回滚上一个版本</strong></p>
<pre><code> kubectl rollout undo deployment/deploy-name
复制代码
</code></pre>
<p><strong>查看历史版本信息</strong></p>
<pre><code>#查看 deployment 升级历史
[root@k8s-m ~]# kubectl rollout history deployment/mydeploy
deployment.extensions/mydeploy 
REVISION  CHANGE-CAUSE
3         none
4         none

#看历史版本更加详细的升级信息
[root@k8s-m ~]# kubectl rollout history deployment/mydeploy  --revision=3
deployment.extensions/mydeploy with revision #3
Pod Template:
  Labels:    pod-template-hash=746bb8db64
    test_node=k8s-node
  Containers:
   myjava-server:
    Image:    registry-vpc.cn-hongkong.aliyuncs.com/aliyun-zhang/javatest:10.0
    Port:    8080/TCP
    Host Port:    0/TCP
    Environment:    none
    Mounts:    none
  Volumes:    none
复制代码
</code></pre>
<p><strong>回滚指定版本</strong></p>
<pre><code>kubectl rollout undo deployment/mydeploy --to-revision=3
复制代码
</code></pre>
<p><strong>回滚指定版本2</strong></p>
<p>直接修改delpoy中的镜像</p>
<h2 id="七-deployment升级策略">七、Deployment升级策略</h2>
<h3 id="1-介绍">1、介绍</h3>
<p>deployment 有 2 种策略，分别是Recreate和RollingUpdate，RollingUpdate是默认的策略RollingUpdate也有相对应的升级策略，如果策略设置的不合理，那么升级的过程就有可能导致服务中断Max Unavailable最多有几个 pod 处于无法工作的状态，默认值是25%Max Surge升级过程中可以比预设的 pod 的数量多出的个数，默认值是25%minReadySeconds等待容器启动的时间，默认值是 0，单位是：秒，容器运行成功之后直接执行下一步根据应用启动时间，设定相应的minReadySeconds，保证应用不中断</p>
<h3 id="2-查看deploy的更新策略">2、查看deploy的更新策略</h3>
<pre><code>[root@k8s-m recreate]# kubectl  explain deploy.spec.strategy.type
KIND:     Deployment
VERSION:  extensions/v1beta1

FIELD:    type string

DESCRIPTION:
     Type of deployment. Can be &quot;Recreate&quot; or &quot;RollingUpdate&quot;. Default is
     RollingUpdate.
复制代码
</code></pre>
<h3 id="3-recreate策略测试">3、recreate策略测试</h3>
<p><strong>结论</strong>：recreate方式会先停止所有就版本，停止完后才部署新版本</p>
<pre><code>[root@k8s-m ~]# kubectl  get pod -w 
NAME                     READY   STATUS    RESTARTS   AGE
my-app-bb9cc7597-dn5bv   1/1     Running   0          3m36s
my-app-bb9cc7597-gqfx4   1/1     Running   0          3m36s
my-app-bb9cc7597-p4dfb   1/1     Running   0          3m36s
my-app-bb9cc7597-p4dfb   1/1     Terminating   0          3m44s
my-app-bb9cc7597-dn5bv   1/1     Terminating   0          3m44s
my-app-bb9cc7597-gqfx4   1/1     Terminating   0          3m44s
my-app-bb9cc7597-p4dfb   0/1     Terminating   0          3m45s
my-app-bb9cc7597-gqfx4   0/1     Terminating   0          3m45s
my-app-bb9cc7597-dn5bv   0/1     Terminating   0          3m45s
my-app-bb9cc7597-p4dfb   0/1     Terminating   0          3m57s
my-app-bb9cc7597-p4dfb   0/1     Terminating   0          3m57s
my-app-db47b56bf-kwd2c   0/1     Pending       0          0s
my-app-db47b56bf-kwd2c   0/1     Pending       0          0s
my-app-db47b56bf-xkqdz   0/1     Pending       0          0s
my-app-db47b56bf-5v4r5   0/1     Pending       0          0s
my-app-db47b56bf-xkqdz   0/1     Pending       0          0s
my-app-db47b56bf-kwd2c   0/1     ContainerCreating   0          0s
复制代码
</code></pre>
<h3 id="4-rollingupdate策略测试">4、RollingUpdate策略测试</h3>
<p><strong>结论:</strong> 一个接一个地以滚动更新方式发布新版本，滚动的方式可以定义，即先删除多少pod在添加多少pod，或者是闲添加在删除。</p>
<pre><code>[root@k8s-m ~]# kubectl get pod  -w
NAME                     READY   STATUS    RESTARTS   AGE
my-app-db47b56bf-2c2c6   1/1     Running   0          19s
my-app-db47b56bf-k7k5z   1/1     Running   0          19s
my-app-db47b56bf-mclj8   1/1     Running   0          19s
my-app-bb9cc7597-hgz6r   0/1     Pending   0          0s
my-app-bb9cc7597-hgz6r   0/1     Pending   0          0s
my-app-bb9cc7597-hgz6r   0/1     ContainerCreating   0          1s
my-app-bb9cc7597-hgz6r   0/1     ContainerCreating   0          1s
my-app-bb9cc7597-hgz6r   0/1     Running             0          5s
my-app-bb9cc7597-hgz6r   1/1     Running             0          6s
my-app-db47b56bf-mclj8   1/1     Terminating         0          29s
my-app-bb9cc7597-84ngs   0/1     Pending             0          0s
my-app-bb9cc7597-84ngs   0/1     Pending             0          0s
my-app-bb9cc7597-84ngs   0/1     ContainerCreating   0          1s
my-app-bb9cc7597-84ngs   0/1     ContainerCreating   0          2s
my-app-db47b56bf-mclj8   0/1     Terminating         0          31s
my-app-db47b56bf-mclj8   0/1     Terminating         0          34s
my-app-db47b56bf-mclj8   0/1     Terminating         0          34s
my-app-bb9cc7597-84ngs   0/1     Running             0          6s
my-app-bb9cc7597-84ngs   1/1     Running             0          7s
复制代码
[root@k8s-m recreate]# kubectl  explain deploy.spec.strategy.rollingUpdate
KIND:     Deployment
VERSION:  extensions/v1beta1

RESOURCE: rollingUpdate Object

DESCRIPTION:
     Rolling update config params. Present only if DeploymentStrategyType =
     RollingUpdate.

     Spec to control the desired behavior of rolling update.

FIELDS:
   maxSurge    string
     The maximum number of pods that can be scheduled above the desired number
     of pods. Value can be an absolute number (ex: 5) or a percentage of desired
     pods (ex: 10%). This can not be 0 if MaxUnavailable is 0. Absolute number
     is calculated from percentage by rounding up. By default, a value of 1 is
     used. Example: when this is set to 30%, the new RC can be scaled up
     immediately when the rolling update starts, such that the total number of
     old and new pods do not exceed 130% of desired pods. Once old pods have
     been killed, new RC can be scaled up further, ensuring that total number of
     pods running at any time during the update is at most 130% of desired pods.

   maxUnavailable    string
     The maximum number of pods that can be unavailable during the update. Value
     can be an absolute number (ex: 5) or a percentage of desired pods (ex:
     10%). Absolute number is calculated from percentage by rounding down. This
     can not be 0 if MaxSurge is 0. By default, a fixed value of 1 is used.
     Example: when this is set to 30%, the old RC can be scaled down to 70% of
     desired pods immediately when the rolling update starts. Once new pods are
     ready, old RC can be scaled down further, followed by scaling up the new
     RC, ensuring that the total number of pods available at all times during
     the update is at least 70% of desired pods.
</code></pre>
<h3 id="5-其它更新策略金丝雀等">5、其它更新策略（金丝雀等）</h3>
<p><a href="https://zhuanlan.zhihu.com/p/55964678">zhuanlan.zhihu.com/p/55964678</a></p>
<p>原文链接：https://juejin.im/post/6844903925124972551</p>

          
          <p class="next-post">下一篇：
            <a href="https://blog.jwangkun.com/post/y5GbzS3T8/">
              <span class="post-title">
                k8s学习资料&rarr;
              </span>
            </a>
          </p>
        
        <div class="comment">
          
        </div>
      </div>
    </div>
  </article>
 <!-- Footer -->
  <footer>
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <ul class="list-inline text-center">
            
            
              
            
              
            
              
            
              
            
              
            
              
            
              
              <li class="list-inline-item">
              <a href="https://blog.jwangkun.com/atom.xml" target="_blank">
                <span class="fa-stack fa-lg">
                  <i class="fas fa-circle fa-stack-2x"></i>
                  <i class="fas fa-rss fa-stack-1x fa-inverse"></i>
                </span>
              </a>
              </li>
          </ul>
          <p class="copyright text-muted">Copyright &copy;<span>John Wong&#39;s Blog</span><br><a href="https://github.com/getgridea/gridea" class="Themeinfo">Powered by Gridea</a></p>
        </div>
      </div>
    </div>
   </footer>
  <!-- Bootstrap core JavaScript -->
  <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
  <!-- <script src="https://blog.jwangkun.com/media/scripts/bootstrap.bundle.min.js"></script> -->
  <!-- Bootstrap core JavaScript -->
  <script src="https://cdn.jsdelivr.net/gh/Alanrk/clean-cdn@1.0/scripts/clean-blog.min.js"></script>
  <!-- <script src="https://blog.jwangkun.com/media/scripts/clean-blog.min.js"></script> -->
  <script src="//instant.page/3.0.0" type="module" defer integrity="sha384-OeDn4XE77tdHo8pGtE1apMPmAipjoxUQ++eeJa6EtJCfHlvijigWiJpD7VDPWXV1"></script>
  <style type="text/css">a.back_to_top{text-decoration:none;position:fixed;bottom:40px;right:30px;background:#f0f0f0;height:40px;width:40px;border-radius:50%;line-height:36px;font-size:18px;text-align:center;transition-duration:.5s;transition-propety:background-color;display:none}a.back_to_top span{color:#888}a.back_to_top:hover{cursor:pointer;background:#dfdfdf}a.back_to_top:hover span{color:#555}@media print,screen and(max-width:580px){.back_to_top{display:none!important}}</style>
<a id="back_to_top" href="#" class="back_to_top">
  <span>▲</span></a>
<script>$(document).ready((function(_this) {
    return function() {
      var bt;
      bt = $('#back_to_top');
      if ($(document).width() > 480) {
        $(window).scroll(function() {
          var st;
          st = $(window).scrollTop();
          if (st > 30) {
            return bt.css('display', 'block')
          } else {
            return bt.css('display', 'none')
          }
        });
        return bt.click(function() {
          $('body,html').animate({
            scrollTop: 0
          },
          800);
          return false
        })
      }
    }
  })(this));
  var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?84ab85460bfbe79dbe5776a1df139a8f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
  </script>
  
<script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1279350888&web_id=1279350888"></script>

  </body>
</html>

